﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.library;
using com.nm.library.Excels;
using System.ComponentModel.Composition;
using com.nm.db.Models.Terminal;
using com.nm;
using george.Helper;
using com.nm.library.Pages;

namespace TerminalAPI.ImportExcel
{
    [Export(typeof(ExcelImport))]
    public class GoodShelfExcel: ExcelImport
    {
        public override ExcelImportType Type => ExcelImportType.BASESHELF;

        /// <summary>
        /// Excel字段映射及校验缓存
        /// </summary>
        /// <returns>字段映射</returns>
        public override Dictionary<string, ImportVerify> DictFields => dictFields;

        /// <summary>
        /// Excel字段映射
        /// </summary>
        private static Dictionary<string, ImportVerify> dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="货架编号",FieldName="GSCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,30,true,true)},
            new ImportVerify{ ColumnName="货架名称",FieldName="GSName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,50,true,true)},
            new ImportVerify{ ColumnName="陈列类别",FieldName="Category",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,6,true,true) },
            new ImportVerify{ ColumnName="代别",FieldName="Generation",DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,10,false,true) },
            new ImportVerify{ ColumnName="投入使用时间",FieldName="InjectDate" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,30,true,false)},
            new ImportVerify{ ColumnName="停止使用时间",FieldName="StopDate",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,30,true,false)}
        }.ToDictionary(e => e.ColumnName, e => e);
       
        /// <summary>
        /// 公共保存数据
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="extraInfo"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            var index = dt.Rows.Count;
            var msg = $"成功导入{index}条数据";
            try
            {
                var dc = new TerminalEntities();
                foreach (DataRow item in dt.Rows)
                {
                    #region 校验
                    var InjectDate = item["InjectDate"].ToString();
                    var StopDate = item["StopDate"].ToString();
                    var gsCode =item["GSCode"].ToString().StartTrim();
                    #endregion
                    var query = dc.Tm_GoodShelf.FirstOrDefault(t => t.GSCode == gsCode);
                    var _InjectDate= Convert.ToDateTime(InjectDate);
                    var _StopDate = Convert.ToDateTime(StopDate);
                    if (query != null)
                    {
                        query.Category = item["Category"].ToString();
                        query.GSCode = gsCode;
                        query.GSName = item["GSName"].ToString();
                        query.Generation = item["Generation"].ToString();
                        query.InjectDate = _InjectDate;
                        query.ModifyDate = DateTime.Now;
                        query.StopDate = _StopDate;
                    }
                    else
                    {
                        var good = new Tm_GoodShelf
                        {
                            Category = item["Category"].ToString(),
                            Generation= item["Generation"].ToString(),
                            CreateDate = DateTime.Now,
                            GSCode = gsCode,
                            GSName = item["GSName"].ToString(),
                            InjectDate =_InjectDate,
                            IsRemove = false,
                            ModifyDate = DateTime.Now,
                            Operator = userId,
                            StopDate = _StopDate
                        };
                        dc.Tm_GoodShelf.Add(good);
                    }
                }
                dc.SaveChanges();
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return new ImportResult()
                {
                    IsSuccess = false,
                    ExtraInfo = ex,
                    Message = ex.Message
                };
            }
            return new ImportResult()
            {
                IsSuccess = true,
                ExtraInfo = msg,
                Message = msg
            };
        }

        public override DataTable GetExportData(string Keys)
        {
            throw new NotImplementedException();
        }
    }
}
